<?php
class wfmModulo {
	public $idModulo = 0;
	public $codModulo = "";
	public $descr = "";
	public $aCampi = array();
	public $aFields = array();
	
	function __construct($idModulo, $codModulo="") {
		global $objDB, $objSess;
		$sSql = "SELECT m.* FROM wfm_moduli m WHERE ";
		if ($idModulo == 0) {
			$sSql .= "m.cod_modulo = '".addSlashes($codModulo)."' ";
		} else {
			$sSql .= "m.id_modulo = ".intval($idModulo)." ";
		}
		$objDB->query($sSql,'wfmModulo');
		if (!$aRec = $objDB->fetch('wfmModulo')) {
			return false;
		}
		$this->idModulo = $aRec['id_modulo'];
		$this->codModulo = $aRec['cod_modulo'];
		$this->descr = $aRec['descr'];
		$this->titolo = $aRec['titolo'];
		$this->aRec = $aRec;
		$this->loadCampi($aRec);
	}
	
	private function loadCampi($aRec) {
		global $objDB, $objSess;
		if (empty($aRec['def_campi_inst'])) {
			$aDefCX = array();
		} else {
			$aDefCX = unserialize($aRec['def_campi_inst']);
		}
		$aDefCampi = array();
		foreach($aDefCX as $sNome => $aCX) {
			$aDefCampi[$sNome] = $aCX;
		}
		$sSql = "SHOW COLUMNS FROM wfm_rec_".$this->codModulo;
		$objDB->exec($sSql,'showCol',false);
		$this->aCampi = array();
		$this->aFields = array();
		while($objDB->errno('showCol')==0 && $aC = $objDB->fetch('showCol')) {
			$sNome = $aC['Field'];
			unset($aC['Field']);
			$this->aFields[$sNome] = $aC;
			if (!isset($aDefCampi[$sNome])) {
				continue;
			}
			$this->aCampi[$sNome] = $aDefCX[$sNome];
		}
	}
	
	public function getCampiList() {
		$aCampi = array();
		foreach($this->aCampi as $sNome => $aDCX) {
			if ($aDCX['lis']!="") {
				$aCampi[$aDCX['lis'].$sNome] = $sNome;
			}
		}
		ksort($aCampi);
		return array_values($aCampi);
	}
	
	public function getCampiAll() {
		return array_merge($this->getCampiFix(),$this->aCampi);
	}
	
	public function getCampiFix () {
		include OVF_FS_WEB_ROOT."/app/wfma/_inc/inc.aCampiFix.php";
		return $aCampiFix;
	}
	
	/**
	 * getHtmlCampiStd
	 * genera l'html della pagina
	 * @param unknown_type $modalita = 'vis', 'mod', 'zap'
	 */
	public function getHtmlCampiStd($modalita='vis') {
		global $objDB, $objSess;
		$sHtml = "";
		$sSql = "SELECT u.* FROM ovf_utenti u "
			." ORDER BY cognome,nome";
		$objDB->query($sSql,'selX');
		$aUtenti = array("0"=>"-- non definito --");
		while($aX = $objDB->fetch('selX')) {
			$aUtenti[$aX['id_utente']] = substr($aX['cognome']." ".$aX['nome'],0,50);
		}
		$sSql = "SELECT s.* FROM wfm_stati s WHERE s.id_modulo = ".$this->idModulo
			." ORDER BY stato";
		$objDB->query($sSql,'selX');
		$aStati = array("0"=>"-- non definito --");
		while($aX = $objDB->fetch('selX')) {
			$aStati[$aX['stato']] = $aX['stato']." - ".substr($aX['descr'],0,40);
		}
		$sSql = "SELECT p.* FROM wfm_pagine p WHERE p.id_modulo = ".$this->idModulo
			." ORDER BY p.cod_pagina";
		$objDB->query($sSql,'selX');
		$aPagine = array("0"=>"** lavorazione terminata **");
		while($aX = $objDB->fetch('selX')) {
			$aPagine[$aX['id_pagina']] = $aX['cod_pagina']." - ".substr($aX['descr'],0,40);
		}
		$sHtml .= "<tr>";
		$sHtml .= "<td class='lab1' width='20%'>Stato</td><td class='txt1' width='30%'>";
		if ($modalita=='zap') {
			$sHtml .= getHtmlSelect('A_stato',$aStati);
		} else {
			$sHtml .= $aStati[reqVar('A_stato')];
		}
		$sHtml .= "</td>";
		$sHtml .= "<td class='lab1' width='20%'>Pagina</td><td class='txt1' width='30%'>";
		if ($modalita=='zap') {
			$sHtml .= getHtmlSelect('A_id_pagina',$aPagine);
		} else {
			$sHtml .= reqVar('A_id_pagina')."-".$aPagine[reqVar('A_id_pagina')];
		}
		$sHtml .= "</td></tr>\n";
		foreach($this->aCampi as $sNomX => $aCX) {
			$sHtml .= "<tr>";
			$sHtml .= "<td class='lab1'>".$this->getCampoLab($sNomX)."</td>";
			$sHtml .= "<td class='txt1' colspan='3'>";
			if ($modalita == 'vis') {
				$sHtml .= $this->getCampoVis($sNomX);
			} else {
				$sHtml .= $this->getCampoMod($sNomX);
			}
			$sHtml .= "</td>";
			$sHtml .= "</tr>\n";
		}
		$sHtml .= "<tr>";
		$sHtml .= "<td class='lab1' width='20%'>Inserito alle</td><td class='txt1' width='80%' colspan='3'>";
		$sHtml .= date('H:i:s \d\e\l d/m/Y',strtotime(reqVar('A_data_ora_ins')))." da ".$aUtenti[reqVar('A_id_utente_ins')];
		$sHtml .= "</td></tr>\n";
		$sHtml .= "<td class='lab1' width='20%'>Modificato alle</td><td class='txt1' width='80%' colspan='3'>";
		$sHtml .= date('H:i:s \d\e\l d/m/Y',strtotime(reqVar('A_data_ora_mod')))." da ".$aUtenti[reqVar('A_id_utente_mod')];
		$sHtml .= "</td></tr>\n";
		return $sHtml;
	}
	
	function getCampoLab($campo) {
		global $objDB, $objSess;
		$sVal = (isset($this->aCampi[$campo]))?$this->aCampi[$campo]:$campo;
		return $sVal;
	}
	
	function getCampoVis($campo,$pref="A_") {
		global $objDB, $objSess;
		$reqVar = $pref.$campo;
		if (!isset($this->aCampi[$campo])) {
			return getHtmlVisText($reqVar);
		}
		$aCX = $this->aCampi[$campo];
		switch($aCX['tip']) {
			case '...':
				$sHtml = getHtmlVisNum($reqVar,$aCX['lun']);
				break;
			case 'testo':
				$sHtml = getHtmlVisNote($reqVar);
				break;
			default:
				$sHtml = getHtmlVisText($reqVar);
				break;
		}
		return $sHtml;
	}
	
	function getCampoMod($campo,$pref="A_") {
		global $objDB, $objSess;
		$reqVar = $pref.$campo;
		if (!isset($this->aCampi[$campo])) {
			return getHtmlText($reqVar,40);
		}
		$aCX = $this->aCampi[$campo];
		switch($aCX['tip']) {
			case 'numero':
				$sHtml = getHtmlTextNum($reqVar,$aCX['lun']);
				break;
			case 'intero':
				$sHtml = getHtmlTextNum($reqVar);
				break;
			case 'data':
				$sHtml = getHtmlDate($reqVar);
				break;
			case 'testo':
				$sHtml = getHtmlTextArea($reqVar,100,3);
				break;
			default:
				$sHtml = getHtmlText($reqVar,$aCX['lun'],$aCX['lun']);
				break;
		}
		return $sHtml;
	}
	
	
	function getStatoDes($stato) {
		global $objDB, $objSess;
		$sSql = "SELECT s.* FROM wfm_stati s WHERE s.id_modulo = ".$this->idModulo." AND stato = ".intval($stato);
		$objDB->query($sSql,'wfmModSta');
		if (!$aRec = $objDB->fetch('wfmModSta')) {
			return "?";
		}
		return $aRec['descr'];
	}
}